package com.gbq.controller;

import com.gbq.redisVo.SynEntity;
import com.gbq.service.BaseService;
import com.gbq.service.RedisService;
import com.gbq.util.BaseUtils;
import com.gbq.util.KafkaSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
public class AppController {

    @Value("${tablelists}")
    private  String tablelists;

    @Autowired
    private BaseService baseService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private KafkaSender kafkaSender;


    @RequestMapping("/test")
    @ResponseBody
    public Object test(@RequestBody SynEntity synEntity){
        baseService.insert(synEntity);
        return "a";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody SynEntity synEntity){

        synEntity.getObj().put("id", UUID.randomUUID().toString());
        synEntity.getObj().put("import_time",new Date());
        synEntity.getObj().put("aging_Date", BaseUtils.strToDate(synEntity.getObj().get("aging_Date").toString()));

        Map<String,String> map = new HashMap<>();
        String resOracle = null;
        String resRedis = null;
        resOracle = baseService.insert(synEntity);
        if (resOracle.equals("oracle failed")){
            map.put("msg",resOracle);
            return map;
        }else {
            resRedis = redisService.put(synEntity);
            map.put("msg",resOracle+","+resRedis);
            return map;
        }

    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody SynEntity synEntity){

        synEntity.getObj().put("import_time",new Date());
        synEntity.getObj().put("aging_Date", BaseUtils.strToDate(synEntity.getObj().get("aging_Date").toString()));
        Map<String,String> map = new HashMap<>();
        String resOracle = null;
        String resRedis = null;
        resOracle = baseService.update(synEntity);
        if (resOracle.equals("oracle failed")){
            map.put("msg",resOracle);
            return map;
        }else {
            resRedis = redisService.update(synEntity);
            map.put("msg",resOracle+","+resRedis);
            return map;
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody SynEntity synEntity){

        Map<String,String> map = new HashMap<>();
        String resOracle = null;
        String resRedis = null;
        resRedis = redisService.delete(synEntity);
        if (resRedis.equals("redis failed")){
            map.put("msg",resRedis);
            return map;
        }else if (resRedis.equals("unsupport syn")){
            resOracle = baseService.delete(synEntity);
            map.put("msg",resOracle+","+resRedis);
            return map;
        }else {
            resOracle = baseService.delete(synEntity);
            map.put("msg",resOracle+","+resRedis);
            return map;
        }
    }
}
